import _regeneratorRuntime from "@babel/runtime/regenerator";
import _asyncToGenerator from "@babel/runtime/helpers/esm/asyncToGenerator";
import _classCallCheck from "@babel/runtime/helpers/esm/classCallCheck";
import _createClass from "@babel/runtime/helpers/esm/createClass";
import _inherits from "@babel/runtime/helpers/esm/inherits";
import _createSuper from "@babel/runtime/helpers/esm/createSuper";
import _applyDecoratedDescriptor from "@babel/runtime/helpers/esm/applyDecoratedDescriptor";

var _dec, _dec2, _class;

import React from 'react';
import { Bind } from 'lodash-decorators';
import { loadIMAsync, watchIMSetComponent } from "../../../customize/IM";
var IM = (_dec = Bind(), _dec2 = Bind(), (_class = /*#__PURE__*/function (_React$Component) {
  _inherits(IM, _React$Component);

  var _super = _createSuper(IM);

  function IM() {
    var _this;

    _classCallCheck(this, IM);

    for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
      args[_key] = arguments[_key];
    }

    _this = _super.call.apply(_super, [this].concat(args));
    _this.state = {
      InstantMessaging: {
        component: null
      }
    };
    return _this;
  }

  _createClass(IM, [{
    key: "componentDidMount",
    value: function componentDidMount() {
      var _this2 = this;

      this.unListener = watchIMSetComponent(function () {
        _this2.importCard().then(function (res) {
          var item = res;

          if (item) {
            if (item.__esModule) {
              var InstantMessaging = item.default;

              _this2.setState({
                InstantMessaging: {
                  component: /*#__PURE__*/React.createElement(InstantMessaging, null)
                }
              });
            } else {
              var _InstantMessaging = item;

              _this2.setState({
                InstantMessaging: {
                  component: /*#__PURE__*/React.createElement(_InstantMessaging, null)
                }
              });
            }
          } else {
            _this2.setState({
              InstantMessaging: {
                component: null
              }
            });
          }
        });
      }, true);
    }
  }, {
    key: "componentWillUnmount",
    value: function componentWillUnmount() {
      if (this.unListener) {
        this.unListener();
      }
    }
  }, {
    key: "importCard",
    value: function () {
      var _importCard = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee() {
        var loadCard;
        return _regeneratorRuntime.wrap(function _callee$(_context) {
          while (1) {
            switch (_context.prev = _context.next) {
              case 0:
                loadCard = null;
                _context.prev = 1;
                _context.next = 4;
                return loadIMAsync('CpaasIM');

              case 4:
                loadCard = _context.sent;
                _context.next = 10;
                break;

              case 7:
                _context.prev = 7;
                _context.t0 = _context["catch"](1);
                loadCard = null;

              case 10:
                return _context.abrupt("return", loadCard);

              case 11:
              case "end":
                return _context.stop();
            }
          }
        }, _callee, null, [[1, 7]]);
      }));

      function importCard() {
        return _importCard.apply(this, arguments);
      }

      return importCard;
    }()
  }, {
    key: "renderCard",
    value: function renderCard() {
      var InstantMessaging = this.state.InstantMessaging;

      if (InstantMessaging && InstantMessaging.component) {
        return /*#__PURE__*/React.createElement("div", null, InstantMessaging.component);
      } else {
        return /*#__PURE__*/React.createElement("div", null);
      }
    }
  }, {
    key: "render",
    value: function render() {
      return /*#__PURE__*/React.createElement("div", null, this.renderCard());
    }
  }]);

  return IM;
}(React.Component), (_applyDecoratedDescriptor(_class.prototype, "importCard", [_dec], Object.getOwnPropertyDescriptor(_class.prototype, "importCard"), _class.prototype), _applyDecoratedDescriptor(_class.prototype, "renderCard", [_dec2], Object.getOwnPropertyDescriptor(_class.prototype, "renderCard"), _class.prototype)), _class));
export { IM as default };